package org.yxt.springcloud.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.yxt.springcloud.dao.OrderMapper;
import org.yxt.springcloud.entity.Order;
import org.yxt.springcloud.service.AccountService;
import org.yxt.springcloud.service.OrderService;
import org.yxt.springcloud.service.StorageService;

import javax.annotation.Resource;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private AccountService accountService;
    @Resource
    private StorageService storageService;

    @GlobalTransactional(name = "order_tx_transactional", rollbackFor = Exception.class)
    @Override
    public void saveOrder(Order order) {
        log.info("---->创建订单");
        orderMapper.saveOrder(order);
        log.info("---->调用服务 扣减 库存");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("---->调用服务 扣减 账号余额");
        accountService.decrease(order.getUserId(), order.getMoney());
        // 修改状态
        orderMapper.updateOrderStatus(order.getId(), 1);
        log.info("---->新建订单完成");
    }

    @Override
    public void updateOrderStatus(int id, int status) {

    }
}
